package UIFree.graphicTools.color
{
	import UIFree.graphicTools.color.palettes.uif_basePaletteView;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class uif_baseColorCtrl extends uif_ecomponent {
		private var _palette		: uif_basePaletteView;
		private var _previewColor	: uif_previewColorSpot;
		private var _selectColor	: uint;
		public function uif_baseColorCtrl(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			createComponents();
		}
		
		protected function createComponents(): void {
			_palette = createPalette(getInner());
			_previewColor = new uif_previewColorSpot(app, 0, 0, 50, style.previewColor);
			_previewColor.setVisibility(hitMouse);
			applyChildsStyle();
			
			_palette.addEvent(MouseEvent.ROLL_OVER, doOver);
			_palette.addEvent(MouseEvent.ROLL_OUT, doOut);
			_palette.addEvent(MouseEvent.MOUSE_MOVE, doMove);
			_palette.addEvent(MouseEvent.MOUSE_DOWN, doClick);
		}
		
		public function get previewColor(): uif_previewColorSpot {
			return _previewColor;
		}
		
		public function get palette(): uif_basePaletteView {
			return _palette;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
		}
		
		protected function createPalette(rect: Rectangle): uif_basePaletteView {
			return new uif_basePaletteView(this, rect.x, rect.y, rect.width, rect.height, style.palette);
		}
		
		protected function updatePreviewPos(): void {
			_previewColor.position = app.mousePos.add(new Point(15, -_previewColor.height / 2));
			updateFocusColor();
		}
		
		protected function updateFocusColor(): void {
			_previewColor.color = focusColor;
		}
		
		protected function doOver(e: Event): void {
			_previewColor.slideShow();
			updatePreviewPos();
		}
		
		protected function doOut(e: Event): void {
			_previewColor.slideHide();
		}
		
		protected function doMove(e: Event): void {
			updatePreviewPos();
		}
		
		protected function doClick(e: Event): void {
			selectColor = focusColor;
		}
		
		public function get focusColor(): uint {
			return _palette.getColor(_palette.mouseX, _palette.mouseY);
		}
		
		public function get selectColor(): uint {
			return _selectColor;
		}
		
		public function set selectColor(a_color: uint): void {
			_selectColor = a_color;
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		override public function dispose():void {
			_previewColor.dispose();
			super.dispose();
		}
	}
}